<template>
	<view class="main">
		<view class="goods">
			<view class="left">
				<image style="width: 100%;" src="http://applet.atptest.com/2023081702.jpg" mode="widthFix">
				</image>
			</view>
			<view class="right">
				<view>
					<text>检测单号：</text>
					<text>{{formData.orderNo}}</text>
				</view>
				<view><text>检测型号：</text><text>{{orderInfo.model}}</text></view>
				<view><text>检测项目：</text><text>{{orderInfo.project}}</text></view>
				<view><text>检测费用：</text><text>￥{{orderInfo.total}}</text></view>
				<view><text>检测日期：</text><text>{{orderInfo.checkTime}}</text></view>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
		<view class="service">
			<view style="margin-bottom: 10rpx;">您对本次检测满意吗?</view>
			<view style="font-size: 24rpx;color: #939393;margin-bottom: 10rpx;">评论为匿名提交</view>
			<view class="box">检测结果：<u-rate :count="5" v-model="formData.evaluation[0]"></u-rate>
			</view>
			<view class="box">检测时效：<u-rate :count="5" v-model="formData.evaluation[1]"></u-rate>
			</view>
			<view class="box">服务质量：<u-rate :count="5" v-model="formData.evaluation[2]"></u-rate>
			</view>
			<u--textarea v-model="formData.content" placeholder="其他问题描述"></u--textarea>
		</view>
		<view style="padding: 0 20rpx;box-sizing: border-box;">
			<button style="color:#fff;width: 100%;border: none;" @click="submit"
				:style="{backgroundColor:btnDisabled ? '#dccfcc' : '#3E56BA'}">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		getEvaluation,
		addEvaluation,
		QueryOrderDetail
	} from "../../../api/order.js"
	export default {
		data() {
			return {
				btnDisabled: false,
				orderInfo: {
					orderNo: "",
					model: "",
					project: "",
					total: "",
					checkTime: ""
				},
				formData: {
					content: '',
					orderNo: null,
					evaluation: []
				}
			};
		},

		onLoad: function(option) {
			this.formData.orderNo = option.i
		},
		onShow() {
			this.getOrderDetail()
			this.getEvaluation()
		},
		methods: {
			//订单详情
			async getOrderDetail() {
				let req = {
					orderNo: this.formData.orderNo
				}
				const res = await QueryOrderDetail(req)
				this.orderInfo = res
			},

			//获取评价内容
			async getEvaluation() {
				let req = {
					orderNo: this.formData.orderNo
				}
				const res = await getEvaluation(req)
				if (res) {
					this.btnDisabled = true
					this.formData = res
				} else {
					this.btnDisabled = false
				}
			},

			//提交评价
			async submit() {
				console.log("this.formData.evaluation", this.formData.evaluation);
				if (this.btnDisabled) {
					this.$refs.uToast.show({
						position: 'top',
						message: '您已评价过此订单，不能重复评价'
					})
					return
				}
				if (this.formData.evaluation.length < 3) {
					this.$refs.uToast.show({
						position: 'top',
						message: '请完善评价内容'
					})
					return
				}

				let res = await addEvaluation(this.formData)
				if (res) {
					uni.showToast({
						duration: 2000,
						title: '提交成功！'
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1 // 可以不写，默认值为 1
						})
					}, 1000)
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.main {
		margin: 10px;

		.goods {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 10rpx 0;
			font-size: 28rpx;

			.left {
				width: 30%;
				padding-right: 10rpx;
			}

			.right {
				flex: 1;

				view {
					margin: 6rpx 0;
				}
			}
		}

		.service {
			margin-top: 20rpx;
			padding: 20rpx;

			.box {
				display: flex;
				margin: 10rpx 0;
			}
		}
	}
</style>